/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package Tools;

import java.awt.Component;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;

/**
 *
 * @author Иван
 */
public class CommandExtraThreadGUI implements Runnable, IExtraThreadFactory {

    public Thread t;
    private ACommand command;
    private String key;
    private Logger logger;
    private Component parentComponent;
    private String msg;
    private int option;

    public CommandExtraThreadGUI(Component parentComponent, int option) {
        this.option = option;
        this.parentComponent = parentComponent;

        logger = Logger.getLogger(CommandExtraThreadIOStream.class);
    }

    public CommandExtraThreadGUI() {
        logger = Logger.getLogger(CommandExtraThreadIOStream.class);
    }

    @Override
    public Thread getThread() {
        return t;
    }

    @Override
    public void initializeExtraThread(ACommand command, String msg, String key) {
        t = new Thread(this, "CommandExtraThread");
        this.command = command;
        this.msg = msg;
        this.key = key;

    }

    @Override
    public void run() {
        try {

            int k = JOptionPane.showConfirmDialog(parentComponent, msg, command.commandName, option, JOptionPane.INFORMATION_MESSAGE);

            logger.info(msg);

            if (k == JOptionPane.OK_OPTION) {
                key = "yes";
            } else if (k == JOptionPane.NO_OPTION) {
                key = "no";
            }

            command.isValidKey(key);
            command.action(key);

            logger.info(key);

        } catch (Exception e) {
            JOptionPane.showConfirmDialog(parentComponent, e.getMessage(), command.commandName, JOptionPane.OK_OPTION, JOptionPane.ERROR_MESSAGE);
            logger.error(e.getMessage(), e);
        }

    }
}
